#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>

#define FIFO_NAME "/tmp/my_fifo"

int main()
{
    int pipe_fd;
    int res;
    int open_mode = O_RDONLY;
    char buffer [PIPE_BUF + 1];
    int bytes_read = 0;
    
    memset(buffer,'\0',sizeof(buffer));

    printf ("Process [%d] opening FIFO O_RDNOLY\n",getpid());
    pipe_fd = open(FIFO_NAME,open_mode);
    printf("process [%d] result [%d]\n",getpid(),pipe_fd);

    if(pipe_fd != -1){
        do{
            res = read(pipe_fd,buffer,PIPE_BUF);
            bytes_read += res;
        }while(res > 0);
        close(pipe_fd);
    }else{
        exit(EXIT_FAILURE);
    }
    printf("process [%d] finished,[%d] bytes read \n",getpid(),bytes_read);
    exit(EXIT_SUCCESS);
}

